<template>
  <transition
    name="custom-classes-transition"
    enter-active-class="animated fadeInDown"
    leave-active-class="animated fadeOutUp"
  >

    <div class="modal-ctrl-body" v-if="RevampModalObj" v-show="RevampModalObj.isShow">
      <div class="modal-ctrl-main odd-row-ctrl">
        <div class="modal-title">
          <p>修改警务通信息</p>
        </div>
        <div class="modal-ctrl-content">

          <div class="modal-row">
            <div class="modal-form-key f-l indispensable">
              设备编号：
            </div>
            <input placeholder="设备编号" readonly="readonly" maxlength="32" type="text" v-model="AddModalData.NUM" class="modal-text f-l">
          </div>

          <div class="modal-row">
            <div class="modal-form-key f-l indispensable">
              所属单位：
            </div>
            <input type="text" v-model="stationName" placeholder="请选择所属单位" @click="getStationTree" readonly="readonly" class="modal-text f-l">
            <input v-model="AddModalData.STATION_ID" type="hidden">
            <div class="sectionTree" v-show="sectionTreeIShow">
              <div id="sectionRevampTree" class="ztree sectionTree-box"></div>
            </div>
          </div>

          <div class="modal-row">
            <div class="modal-form-key f-l">
              电话：
            </div>
            <input placeholder="电话" type="text" maxlength="32" v-model="AddModalData.TEL_NUM" class="modal-text f-l">
          </div>

          <div class="modal-row">
            <div class="modal-form-key f-l">
              在线状态：
            </div>
            <select @change="selectStatus" :value="AddModalData.STATUS">
              <option value="1">在线</option>
              <option value="2">离线</option>
            </select>
          </div>

          <div class="modal-row">
            <div class="modal-form-key f-l">
              工作状态：
            </div>
            <select @change="selectIsWork" :value="AddModalData.IS_WORK">
              <option value="1">正常</option>
              <option value="2">损坏</option>
            </select>
          </div>

        </div>
        <div class="btns text-r">
          <button type="button" class="btn btn-default" @click="close">取消</button>
          <button type="button" class="btn btn-info" @click="subBtn">确定</button>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
    export default {
        name: "RevampApp",
        props:['RevampModalObj',"selectRowId"],
        data(){
          return{
            AddModalData:{
              NUM:'',
              STATION_ID:'',
              STATUS:'1',
              IS_WORK:'1',
              TEL_NUM:'',
            },
            stationName:'',
            setting : {
              data: {
                simpleData: {
                  enable: true
                },
              }
            },
            sectionTreeIShow:false,
            appId:""
          }
        },
        watch:{
          RevampModalObj:{
            handler(v1){
              if( v1.isShow ){
                this.appId = this.selectRowId;
                this.getResourceData();
              }
            },
            deep:true
          }
        },
        mounted(){
          let _this = this;
          this.setting.callback = {
            onClick:function(event,treeId,treeNode){ //新增警员部门树选择
              //console.log(treeNode);
              _this.AddModalData.STATION_ID = treeNode.id;
              _this.stationName = treeNode.name;
              _this.sectionTreeIShow = !_this.sectionTreeIShow;
            }
          };
          this.$axios.get(this.HOST+'/station/getOrgaInfo')
            .then((data)=>{
              $.fn.zTree.init($("#sectionRevampTree"), this.setting, data.data.data);
              //console.log(data);
            })
            .catch((err)=>{
              //console.log(err);
            })
        },
        methods:{
          //获取详细数据
          getResourceData(){
            //console.log(this.appId);
            let params = {
              "columns":["*"],
              "conditions": [
                {
                  "field": "ID",
                  "symbol": "=",
                  "value": this.appId
                }
              ],
              "dataTypeCode": "policefacility",
              "tableName": "T_RES_JWT"
            };
            this.$axios.post(this.HOST+"/base/query/page",params)
              .then((data)=>{
                //console.log(data);
                let successData = data.data.data.list[0];
                for( let radioDataKey in this.AddModalData ){
                  for( let successKey in successData ){
                    if( radioDataKey === successKey ){
                      this.AddModalData[radioDataKey] =  successData[successKey];
                    }
                  }
                }
                this.stationName = successData.STATION_ID_PARSE;
                //console.log(this.AddModalData);
              })
              .catch((err)=>{
                //console.log(err);
              })
          },
          //提交
          subBtn(){
            //console.log(this.AddModalData);
            let searchParams = {
              "columns":["*"],
              "conditions":[
                {
                  "field": "ID",
                  "symbol": "!=",
                  "value": this.selectRowId
                },
                {"field": "NUM","symbol":"=","value":this.AddModalData.NUM}
              ],
              "dataTypeCode":"policefacility",
              "pageIndex":1,
              "pageSize":20,
              "tableName":"T_RES_JWT"
            };
            this.$axios.post(this.HOST+"/base/query/page",searchParams)
              .then((data)=>{
                if( data.data.data.list.length > 0 ){
                  this.s_alert("已存在该设备编号！");
                }else{
                  if( this.verifyForm.isNull(this.AddModalData.NUM) ){
                    this.s_alert("警务通编号为空或者格式不正确！");
                    return false;
                  }else if( this.verifyForm.isNull(this.AddModalData.STATION_ID) ){
                    this.s_alert("所属单位不能为空！");
                    return false;
                  }else if( this.verifyForm.iphone(this.AddModalData.TEL_NUM) ){
                    this.s_alert("电话格式不正确！");
                    return false;
                  }

                  let paramsKey = [];
                  let paramsValue = [];

                  for( let key in this.AddModalData ){
                    paramsKey.push(key);
                    paramsValue.push(this.AddModalData[key]);
                  }

                  let params = {
                    "conditions": [
                      {
                        "field": "ID",
                        "symbol": "=",
                        "value": this.appId
                      }
                    ],
                    "dataTypeCode": "policefacility",
                    "data": this.AddModalData,
                    "tableName": "T_RES_JWT"
                  };
                  //console.log(params);
                  this.$axios.put(this.HOST+"/base/modify",params)
                    .then((data)=>{
                      //console.log(data);
                      if( data.data.success ){
                        this.s_alert("修改成功！");
                        this.s_logwrite("资源信息管理-警务通管理","修改警务通");
                        this.close()
                      }else{
                        this.s_alert("修改失败！")
                      }
                    })
                    .catch((err)=>{
                      //console.log(err);
                    })
                }
              })
              .catch((err)=>{
                //console.log(err);
              });
          },
          //状态
          selectStatus(ele){
            ////console.log(ele.target.value);
            this.AddModalData.STATUS = ele.target.value
          },
          //工作
          selectIsWork(ele){
            ////console.log(ele.target.value);
            this.AddModalData.IS_WORK = ele.target.value
          },
          //展开部门树
          getStationTree(){
            this.sectionTreeIShow = !this.sectionTreeIShow;
          },
          //关闭弹窗
          close(){
            this.$emit("RevampRadioModalClose");
            this.clearData(); //关闭弹窗清除数据
          },
          //清除数据
          clearData(){
            this.AddModalData={
              NUM:'',
              STATION_ID:'',
              STATUS:'1',
              IS_WORK:'1',
              TEL_NUM:'',
            };
            this.stationName='';
            this.sectionTreeIShow = false;
          }
        }
    }
</script>

<style scoped>
  .sectionTree{
    position: absolute;
    top: 30px;
    right: 0;
    width: 80%;
    z-index: 2;
    background-color: #fff;
    border:1px solid rgba(51,51,51,0.2);
    box-sizing: border-box;
    padding: 5px;
    max-height: 200px;
    overflow-y: auto;
  }
  .fadeInDown,.fadeOutUp{
    animation-duration: .500s;
  }
</style>
